<?php

namespace App\Http\Controllers;

use App\Peference;
use App\User;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;

class ListController extends Controller
{
    //
    public function patienList(){
        $user=Peference::select(['id','name','age','city','sex','had_before'])->paginate($_GET['limit']);
        $list=$user->toArray();
        $data=[];
        foreach ($list['data'] as $k){
            $k['had_before']=($k['had_before']=='0')?'没有(Never)':'有过(had)';
            $k['sex']=($k['sex']=='0')?'男性(Man)':'女性(Woman)';
            $data[]=$k;
        }
        return response()->json(['code'=>'0','msg'=>'ok','count'=>$list['total'],'data'=>$data]);
    }
    public function selectKeyWord($name){
        $user=Peference::where('name','like','%'.$name.'%')->select(['id','name','age','city','sex','had_before'])->paginate($_GET['limit']);
        $list=$user->toArray();
        $data=[];
        foreach ($list['data'] as $k){
            $k['had_before']=($k['had_before']=='0')?'没有(Never)':'有过(had)';
            $k['sex']=($k['sex']=='0')?'男性(Man)':'女性(Woman)';
            $data[]=$k;
        }
        return response()->json(['code'=>'0','msg'=>'ok','count'=>$list['total'],'data'=>$data]);
    }
    public function addPatienData(Request $request){
        $validator=\Validator::make($request->all(),[
            'name'  =>'required',
            'age' =>'required',
            'city' =>'required',
            'sex' =>'required',
//            'vercode'=>'required'
        ],['required'=>':attribute不能为空'],['name'=>'姓名','age'=>'年龄','city'=>'居住城市']);
        if($validator->fails()){
            return response()->json(['code'=>'1','msg'=>$validator->errors()->first(),'data'=>'']);
        }
        try {
            $user = JWTAuth::toUser($request->input('access_token'));
        } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
            return response()->json(['code'=>'1001','msg' => 'token_invalid', 'data' => '']);
        } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
            return response()->json(['code'=>'1001','msg' => '登录过期,请重新登录', 'data' => '']);
        }

        $data=$request->all();
        unset($data['access_token']);
        $data['uid']=$user->id;
        if(Peference::create($data)){
            return response()->json(['code'=>'0','msg'=>'添加成功','data'=>'']);
        }else{
            return response()->json(['code'=>'1','msg'=>'添加失败','data'=>'']);
        }

    }
    public function getDataList($id){
        $peference=Peference::find($id);
        if(!$peference) return response()->json(['msg'=>'找不到该患者资料','code'=>'1','data'=>'']);
        return response()->json(['msg'=>'ok','code'=>'0','data'=>$peference]);
    }
    public function updatePatienData(Request $request){
        $validator=\Validator::make($request->all(),[
            'name'  =>'required',
            'age' =>'required',
            'city' =>'required',
            'sex' =>'required',
//            'vercode'=>'required'
        ],['required'=>':attribute不能为空'],['name'=>'姓名','age'=>'年龄','city'=>'居住城市']);
        if($validator->fails()){
            return response()->json(['code'=>'1','msg'=>$validator->errors()->first(),'data'=>'']);
        }
        $peference=Peference::find($request->input('id'));
        $data=$request->all();
        unset($data['id']);
        if($peference->update($data)){
            return response()->json(['code'=>'0','msg'=>'修改成功','data'=>'']);
        }else{
            return response()->json(['code'=>'1','msg'=>'修改失败','data'=>'']);
        }
    }
    public function delete($id){
        if(Peference::destroy([$id])){
            return response()->json(['code'=>'0','msg'=>'删除成功','data'=>'']);
        }else{
            return response()->json(['code'=>'1','msg'=>'删除失败','data'=>'']);
        }
    }
    public function test(){
        $user=User::find(1);
        dd($user);
    }
    public function uploadImages(Request $request)
    {   header('Access-Control-Allow-Origin:*');
        $upFile = $_FILES['file'];
        $filename = $_FILES['file']['name'];
        $name=explode('.',$filename);
        return $filename.':=='.$name;
        $querypath='upload/'.md5(rand('100000','999999').time()).'.'.$name['1'];
        if(move_uploaded_file($_FILES['file']['tmp_name'],env("IMAGE_URL").'/'.$querypath)){
            return ['images'=>'/'.$querypath];
        }

    }
}
